//
//  CreateStroyViewController.m
//  ImageStory
//
//  Created by 大帅逼 on 16/1/6.
//  Copyright © 2016年 helloApp. All rights reserved.
//

#import "CreateStroyViewController.h"
#import "WitleViewController.h"
#import "CreateManagerViewController.h"
#import "Header.h"
#import "MBProgressHUD+PKX.h"
#import "CreateTableViewCell.h"
#import "FilterObject.h"
#import <BmobSDK/Bmob.h>
#import <BmobSDK/BmobProFile.h>
#import "SuccessViewController.h"
#import "UserInfoModal.h"
#import "LocaData.h"
#import "LoginViewController.h"
#import "ImageProcessViewController.h"

#define MARGIN 20

@interface CreateStroyViewController ()<UITextViewDelegate,UITableViewDataSource,UITableViewDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate,UITextFieldDelegate>{

    NSString    *_time;
    NSString    *_address;
    NSString    *_mark;
}

@property(nonatomic,strong) UITextField             *titleTextField;
@property(nonatomic,strong) UITextView              *contextTextView;
@property(nonatomic,strong) UIImageView             *addImageView;
@property(nonatomic,strong) UIView                  *headView;

@property(nonatomic,strong) UITableView             *createTableView;
@property(nonatomic,strong) UIAlertController       *imageAlertController;
@property(nonatomic,strong) UIImagePickerController *imagePicker;
@property(nonatomic,strong) NSArray                 *optionIconArray;
@property(nonatomic,strong) NSArray                 *optionTitltArray;
@property(nonatomic,strong) UIDatePicker            *datePicker;
@property(nonatomic,strong) UIView                  *timeView;
@property(nonatomic,strong) UIButton                *sureBtn;
@property(nonatomic,strong) NSMutableArray          *cellArray;
@property(nonatomic,strong) UIView                  *progressView;
@property(nonatomic,strong) UILabel                 *progressLabel;
@property(nonatomic,strong) UIView                  *maskView;
@property(nonatomic,strong) UIActivityIndicatorView *activityView;
@property(nonatomic,strong) UIAlertController       *alertController;

@end

@implementation CreateStroyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 设置导航控制器样式
    [self initNavigation];
    
    // 初始化页面
    [self initView];
    
    // 初始化通知
    [self initNotification];
    
}
/* - - - - - - - - - - - - - - -设置导航控制器样式 - - - - - - - - - - - -  */

- (void) initNavigation {
    
    [self.navigationController.navigationBar setBarTintColor:[UIColor blackColor]];
    
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor], NSFontAttributeName : [UIFont systemFontOfSize:20]}];
    
    self.title = @"写故事";
    
    // 设置导航控制器左右按钮
    UIBarButtonItem *backItme = [[UIBarButtonItem alloc] initWithTitle:@"返回 " style:UIBarButtonItemStylePlain target:self action:@selector(action_backItmeClickEvent)];
    
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
    
    self.navigationItem.leftBarButtonItem = backItme;
    
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithTitle:@"发布" style:UIBarButtonItemStylePlain target:self action:@selector(action_createItemClickEvent)];
    
    self.navigationItem.rightBarButtonItem = rightItem;
}

/* - - - - - -- - - - - - - - -页面相关设置 - - - - - - - - - -- -  */

- (void) initView {
    
    _cellArray = [[NSMutableArray alloc] init];

    [self.view setBackgroundColor:Bg_Color];
    
    [self.view addSubview:self.createTableView];
    
    [self.view addSubview:self.timeView];
    
    [self.view addSubview:self.maskView];
    
    [self.view addSubview:self.progressView];
}

- (void) initNotification {
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(action_chanceImageBack :) name:@"chanceImage" object:nil];
    
    // 添加正文的通知  
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(action_setContextString :) name:@"contextString" object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(action_addToimageStrory :) name:@"addToImageStory" object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(action_setAddress:) name:@"clickAddress" object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(action_setMark:) name:@"setMark" object:nil];
}

/* - - - - - -- - - - - - - - -数据源方法相关设置 - - - - - - - - - -- -  */

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.optionTitltArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    CreateTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CREATE_CELL"];
    
    if (!cell) {
        
        cell = [[CreateTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CREATE_CELL"];
    }
    
    [cell setBackgroundColor:[UIColor colorWithRed:44/255.0 green:45/255.0 blue:49/255.0 alpha:1]];
    
    // 设置数据
    [cell.iconImageView setImage:[UIImage imageNamed:[self.optionIconArray objectAtIndex:indexPath.row]]];
    
    [cell.titleLabel setText:[self.optionTitltArray objectAtIndex:indexPath.row]];
    
    if (indexPath.row != 0) {
        
        [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
    }
    
    if (indexPath.row == 2) {
        
        [cell.lineView setBackgroundColor:[UIColor clearColor]];
    }
    
    [_cellArray addObject:cell];
    
    return cell;
}

/* - - - - - -- - - - - - - - -代理事件相关设置 - - - - - - - - - -- -  */

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return My_Height(20);
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.row == 0) {
        
        [self action_timeClickEvent];
    }else if(indexPath.row == 1){
    
        [[CreateManagerViewController shardManger] showLocationViewController];
    }else {
        
        [[CreateManagerViewController shardManger] showStyleViewController];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    return My_Height(50);
}

#pragma mark -- 图片选取器的代理事件
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    UIImage *selectImage = info[UIImagePickerControllerOriginalImage];
    
    // 退出相片控制器
    [self dismissViewControllerAnimated:YES completion:nil];
    
    [ImageProcessViewController shardImagePrecessViewController].isNewImage = @"yes";
    
    [[CreateManagerViewController shardManger] showImageProcessViewControllerWithImage:selectImage];
}

/* - - - - - -- - - - - - - - -点击事件相关设置 - - - - - - - - - -- -  */

// 点击返回按钮事件
- (void) action_backItmeClickEvent {

    [self dismissViewControllerAnimated:YES completion:nil];
}

// 点击发布按钮事件
- (void) action_createItemClickEvent {
    
    if (self.titleTextField.text.length > 0 && self.contextTextView.text > 0 && self.addImageView.image != nil) {
        
        if ([LocaData isLogin]) {
            
            [self upDataToService];
        }else {
        
            [self presentViewController:self.alertController animated:YES completion:nil];
        }
    }else {
    
        [MBProgressHUD showError:@"请补全信息"];
    }
}

#pragma mark -- 上传数据
- (void) upDataToService {

    NSData *imageData;
    
    if (UIImagePNGRepresentation(self.addImageView.image) == nil) {
        
        imageData = UIImageJPEGRepresentation(self.addImageView.image, 1);
    }else {
        
        imageData = UIImagePNGRepresentation(self.addImageView.image);
    }
    
    [self.navigationItem.rightBarButtonItem setEnabled:NO];
    
    [self showProgressView];
    
    // 在异步线程中上传图片
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        [BmobProFile uploadFileWithFilename:@"image.jpg" fileData:imageData block:^(BOOL isSuccessful, NSError *error, NSString *filename, NSString *url,BmobFile *bmobFile) {
            if (isSuccessful) {
                
                [BmobProFile getFileAcessUrlWithFileName:filename callBack:^(BmobFile *file, NSError *error) {
                    if (error) {
                        
                        [MBProgressHUD showError:@"获取文件地址失败"];
                    } else {
                        
                        BmobObject *imageStoryObject = [BmobObject objectWithClassName:@"imageStory"];
                        
                        if (self.titleTextField.text.length > 8) {
                            
                            [imageStoryObject setObject:[self.titleTextField.text substringToIndex:8] forKey:@"imageStoryName"];
                        }else {
                        
                            [imageStoryObject setObject:self.titleTextField.text forKey:@"imageStoryName"];
                        }
                        
                        [imageStoryObject setObject:self.contextTextView.text forKey:@"imageStoryContext"];
                        
                        [imageStoryObject setObject:file.url forKey:@"imageStoryImage"];
                        
                        [imageStoryObject setObject:_time forKey:@"imageStoryTime"];
                        
                        [imageStoryObject setObject:_address forKey:@"imageStoryLocation"];
                        
                        [imageStoryObject setObject:[NSString stringWithFormat:@"%f",self.addImageView.image.size.width] forKey:@"imageWidth"];
                        
                        [imageStoryObject setObject:[NSString stringWithFormat:@"%f",self.addImageView.image.size.height] forKey:@"imageHeight"];
                        
                        [imageStoryObject setObject:self.titleTextField.text forKey:@"imageStoryStyle"];
                        
                        [imageStoryObject setObject:[LocaData readUserInfo].userName forKey:@"imageStoryUser"];
                        
                        [imageStoryObject saveInBackgroundWithResultBlock:^(BOOL isSuccessful, NSError *error) {
                            if (isSuccessful) {
                                
                                [self showProgressView];
                                
                                [self.navigationItem.rightBarButtonItem setEnabled:YES];
                                
                                SuccessViewController *successVC = [[SuccessViewController alloc] init];
                                
                                [self.navigationController pushViewController:successVC animated:YES];
                                
                                self.titleTextField.text = nil;
                                self.addImageView.image = [UIImage imageNamed:@"hourly"];
                                self.contextTextView.text = nil;
                                for (int i = 0; i < _cellArray.count; i++) {
                                    
                                    CreateTableViewCell *cell = [_cellArray objectAtIndex:i];
                                    
                                    cell.msgLabel.text = nil;
                                }
                                
                            } else {
                                NSLog(@"%@",error);
                                
                                [self.navigationItem.rightBarButtonItem setEnabled:YES];
                                
                                [MBProgressHUD showError:[NSString stringWithFormat:@"数据上传失败%@",error]];
                                [self showProgressView];
                            }
                        }];
                    }
                }];
                
            } else {
                if (error) {
                    NSLog(@"%@",error);
                    [self.navigationItem.rightBarButtonItem setEnabled:YES];
                    
                    [MBProgressHUD showError:[NSString stringWithFormat:@"图片上传失败%@",error]];
                    [self showProgressView];
                }
            }
        } progress:^(CGFloat progress) {
            
            //上传进度，此处可编写进度条逻辑
            [_progressLabel setText:[NSString stringWithFormat:@"当前进度%.0f％",progress * 100]];
        }];
    });
}

- (void) showProgressView {

    if (self.progressView.alpha > 0) {
        
        [UIView animateWithDuration:0.5 animations:^{
            
            [self.activityView stopAnimating];
            
            [self.maskView setAlpha:0];
           
            [self.progressView setAlpha:0];
            
        }];
    }else {
    
        [UIView animateWithDuration:0.5 animations:^{
            
            [self.maskView setAlpha:1];
            
            [self.progressView setAlpha:1];
            
            [self.activityView startAnimating];
        }];
    }
}

#pragma mark -- 点击事件
// 添加图片的点击事件
- (void) action_chanceImage {
    
    [self presentViewController:self.imageAlertController animated:YES completion:nil];
}

// 修改图片后的返回事件
- (void) action_chanceImageBack :(NSNotification *) notification {
    
    [self.addImageView setImage:notification.object];
}

// 开始编辑正文的点击事件
- (void) action_startWitle {

    [[CreateManagerViewController shardManger] showWitleView];
}

// 点击事件后的事件
- (void) action_timeClickEvent {
    
    [self showTimeView];
}

// 确定时间后的点击事件
- (void) action_sureClickEvent {
    
    CreateTableViewCell *cell = [_cellArray objectAtIndex:0];
    
    // 获取用户通过UIDatePicker设置的日期和时间
    NSDate *selected = [_datePicker date];
    
    // 创建一个日期格式器
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    
    // 为日期格式器设置格式字符串
    [dateFormatter setDateFormat:@"yyyy年MM月dd日"];
    
    // 使用日期格式器格式化日期、时间
    NSString *destDateString = [dateFormatter stringFromDate:selected];
    
    [cell.msgLabel setText:destDateString];
    
    _time = destDateString;
    
    [self closeTimeView];
}

// 确定正文后的通知事件
- (void) action_setContextString : (NSNotification *) notification{

    [self.contextTextView setTextColor:[UIColor blackColor]];
    [self.contextTextView setText:notification.object];
}

// 点击视图关闭键盘
- (void) action_closeKeyBoard {
    
    [self.view endEditing:YES];
}

- (void) action_addToimageStrory : (NSNotification *)sender{

    [self.addImageView setImage:sender.object];
}

- (void) action_setAddress : (NSNotification *)sender{

    CreateTableViewCell *cell = [_cellArray objectAtIndex:1];
    
    _address = sender.object;
    
    [cell.msgLabel setText:sender.object];
}

- (void) action_setMark : (NSNotification *) sender {

    CreateTableViewCell *cell = [_cellArray objectAtIndex:2];
    
    NSString *oldString = cell.msgLabel.text;
    
    NSString *newString = sender.object;
    
    _mark = [NSString stringWithFormat:@"%@、%@",oldString,newString];
    
    if (oldString.length > 0) {
        
        [cell.msgLabel setText:[NSString stringWithFormat:@"%@、%@",oldString,newString]];
    }else {
    
        [cell.msgLabel setText:newString];
    }
}

#pragma mark -- 时间选择视图的显示与关闭
- (void) showTimeView {
    
    [UIView animateWithDuration:0.3 animations:^{
        
        [self.timeView setFrame:CGRectMake(0, SCREEN_H - My_Height(240), SCREEN_W, My_Height(240))];
    } completion:nil];
}

- (void) closeTimeView {
    
    
    [UIView animateWithDuration:0.3 animations:^{
        
        [self.timeView setFrame:CGRectMake(0, SCREEN_H + My_Height(240), SCREEN_W, My_Height(240))];
    } completion:nil];
}

#pragma mark -- 输入框的代理事件
- (void)textFieldDidEndEditing:(UITextField *)textField {

    if (textField.text.length > 8) {
        
        [MBProgressHUD showError:@"最多输入8字哦"];
    }
}

#pragma mark -- 懒加载

- (UIView *)headView {
    
    if (!_headView) {
        
        _headView = [[UIView alloc] initWithFrame:CGRectMake(0, My_Height(20), SCREEN_W, My_Height(300))];
        
        [_headView setBackgroundColor:[UIColor whiteColor]];
        
        [_headView addSubview:self.titleTextField];
        
        UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(MARGIN, My_Height(59.5), SCREEN_W, 0.5)];
        
        [lineView setBackgroundColor:[UIColor whiteColor]];
        
        [lineView setAlpha:0.5];
        
        [_headView setBackgroundColor:[UIColor colorWithRed:44/255.0 green:45/255.0 blue:49/255.0 alpha:1]];
        
        [_headView addSubview:lineView];
        
        [_headView addSubview:self.contextTextView];
        
        [_headView addSubview:self.addImageView];
    }
    return _headView;
}

- (UITextField *)titleTextField {
    
    if (!_titleTextField) {
        
        _titleTextField = [[UITextField alloc] initWithFrame:CGRectMake(0, 10, SCREEN_W, My_Height(40))];
        
        [_titleTextField setLeftViewMode:UITextFieldViewModeAlways];
        
        [_titleTextField setPlaceholder:@"给你的故事取一个标题"];
        
        [_titleTextField setFont:[UIFont systemFontOfSize:16]];
        
        [_titleTextField setLeftView:[[UIView alloc] initWithFrame:CGRectMake(0, 0, MARGIN + 2, My_Height(40))]];
        
        [_titleTextField setBackgroundColor:[UIColor colorWithRed:44/255.0 green:45/255.0 blue:49/255.0 alpha:1]];
        
        [_titleTextField setTextColor:[UIColor colorWithRed:212/255.0 green:212/255.0 blue:216/255.0 alpha:1]];
        
        [_titleTextField setValue:[UIColor colorWithRed:212/255.0 green:212/255.0 blue:216/255.0 alpha:1] forKeyPath:@"_placeholderLabel.textColor"];
        
        [_titleTextField setDelegate:self];
    }
    
    return _titleTextField;
}

- (UITextView *)contextTextView {

    if (!_contextTextView) {
        
        _contextTextView = [[UITextView alloc] initWithFrame:CGRectMake(MARGIN, My_Height(80), My_Height(SCREEN_W - My_Height(40)), My_Height(100))];
        
        [_contextTextView setText:@"经历与感想..."];
        
        [_contextTextView setFont:[UIFont systemFontOfSize:16]];
        
        [_contextTextView setTextColor:[UIColor colorWithRed:212/255.0 green:212/255.0 blue:216/255.0 alpha:1]];
        
        UITapGestureRecognizer *contextGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(action_startWitle)];
        
        [_contextTextView setBackgroundColor:[UIColor colorWithRed:44/255.0 green:45/255.0 blue:49/255.0 alpha:1]];
        
        [_contextTextView addGestureRecognizer:contextGesture];

    }
    return _contextTextView;
}

- (UIImageView *)addImageView {
    
    if (!_addImageView) {
        
        _addImageView = [[UIImageView alloc] initWithFrame:CGRectMake(MARGIN, My_Height(200), My_Height(80), My_Height(80))];
        
        [_addImageView setImage:[UIImage imageNamed:@"hourly"]];
        
        [_addImageView setUserInteractionEnabled:YES];
        
        [_addImageView.layer setCornerRadius:5];
        
        [_addImageView.layer setMasksToBounds:YES];
        
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(action_chanceImage)];
        
        [_addImageView addGestureRecognizer:tapGesture];
    }
    
    return _addImageView;
}

- (UITableView *)createTableView {
    
    if (!_createTableView) {
        
        _createTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, SCREEN_H) style:UITableViewStyleGrouped];
        
        [_createTableView setDataSource:self];
        
        [_createTableView setDelegate:self];
        
        [_createTableView setBackgroundColor:Bg_Color];
        
        [_createTableView setTableFooterView:[[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)]];
        
        [_createTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
        
        [_createTableView setRowHeight:My_Height(50)];
        
        UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, My_Height(320))];
        
        [headView addSubview:self.headView];
        
        [_createTableView setTableHeaderView:headView];
        
        UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];

        [_createTableView setTableFooterView:footerView];
    }
    return _createTableView;
}

- (UIAlertController *)imageAlertController {

    if (!_imageAlertController) {
        
        _imageAlertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"请选择添加图拍片的方式 " preferredStyle:UIAlertControllerStyleActionSheet];
        
        UIAlertAction *cameraBtn = [UIAlertAction actionWithTitle:@"相机" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]){
            
                [self.imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
                
                [self presentViewController:_imagePicker animated:YES completion:nil];
            }else {
            
                [MBProgressHUD showError:@"你的设备没有摄像头"];
            }
        }];
        
        UIAlertAction *photoBtn = [UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum]) {
                [self.imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
                
                // 跳转到图片选择器
                [self presentViewController:_imagePicker animated:YES completion:nil];
                
            }else {
                NSLog(@"你没有获得权限");
            }
        }];
        
        UIAlertAction *calcelBtn = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
        
        [_imageAlertController addAction:cameraBtn];
        
        [_imageAlertController addAction:photoBtn];
        
        [_imageAlertController addAction:calcelBtn];
    }
    return _imageAlertController;
}

- (UIImagePickerController *) imagePicker {
    if (!_imagePicker) {
        _imagePicker = [[UIImagePickerController alloc] init];
        [_imagePicker setDelegate:self];
    }
    return _imagePicker;
}

- (NSArray *)optionIconArray {
    
    if (!_optionIconArray) {
        
        _optionIconArray = [NSArray arrayWithObjects:@"ic_subscribe_time",@"ic_subscribe_location",@"ic_subscribe_require", nil];
    }
    return _optionIconArray;
}

- (NSArray *)optionTitltArray {
    
    if (!_optionTitltArray) {
        
        _optionTitltArray = [NSArray arrayWithObjects:@"时间",@"位置",@"标签", nil];
    }
    return _optionTitltArray;
}

- (UIView *)timeView {
    
    if (!_timeView) {
        
        _timeView = [[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_H + My_Height(240), SCREEN_W, My_Height(240))];
        
        [_timeView setBackgroundColor:[UIColor colorWithWhite:0.95 alpha:1]];
        
        UIView *topLine = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, 0.5)];
        
        [topLine setBackgroundColor:[UIColor grayColor]];
        
        [topLine setAlpha:0.5];
        
        [_timeView addSubview:topLine];
        
        _datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 40, SCREEN_W, My_Height(200))];
        
        [_datePicker setBackgroundColor:[UIColor whiteColor]];
        
        [_timeView addSubview:_datePicker];
        
        _sureBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        
        [_sureBtn setTitle:@"确定 " forState:UIControlStateNormal];
        
        [_sureBtn setFrame:CGRectMake(SCREEN_W - 60, 0, 40, 40)];
        
        [_sureBtn addTarget:self action:@selector(action_sureClickEvent) forControlEvents:UIControlEventTouchUpInside];
        
        [_timeView addSubview:_sureBtn];
    }
    
    return _timeView;
}

- (UIView *)progressView {

    if (!_progressView) {
        
        _progressView = [[UIView alloc] init];
        
        [_progressView setBounds:CGRectMake(0, 0, My_Height(120), My_Height(100))];
        
        [_progressView setCenter:self.view.center];
        
        [_progressView setAlpha:0];
        
        [_progressView setBackgroundColor:[UIColor whiteColor]];
        
        [_progressView.layer setCornerRadius:My_Height(10)];
        
        _activityView = [[UIActivityIndicatorView alloc] init];
        
        [_activityView setBounds:CGRectMake(0, 0, My_Height(60), My_Height(60))];
        
        [_activityView setCenter:CGPointMake(My_Height(60), My_Height(40))];
        
        [_activityView setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleGray];
        
        [_progressView addSubview:_activityView];
        
        _progressLabel = [[UILabel alloc] init];
        
        [_progressLabel setFrame:CGRectMake(0, My_Height(80), My_Height(120), My_Width(20))];
        
        [_progressLabel setFont:[UIFont systemFontOfSize:12]];
        
        [_progressLabel setTextColor:[UIColor grayColor]];
        
        [_progressLabel setTextAlignment:NSTextAlignmentCenter];
        
        [_progressView addSubview:_progressLabel];
    }
    
    return _progressView;
}

- (UIView *)maskView {

    if (!_maskView) {
        
        _maskView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, SCREEN_H)];
        
        [_maskView setBackgroundColor:[UIColor colorWithWhite:0.6 alpha:0.8]];
        
        [_maskView setAlpha:0];
    }
    
    return _maskView;
}

- (UIAlertController *)alertController {

    if (!_alertController) {
        
        _alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"你还没有登录" preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *cancelBtn = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
        
        UIAlertAction *loginBtn = [UIAlertAction actionWithTitle:@"去登录" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            LoginViewController *loginVC = [[LoginViewController alloc] init];
            
            loginVC.isPresent = @"yes";
            
            [self.navigationController pushViewController:loginVC animated:YES];
        }];
        
        [_alertController addAction:cancelBtn];
        
        [_alertController addAction:loginBtn];
    }
    
    return _alertController;
}

@end
